package models

import (
	"gitee.com/lzqysoft/framework/common/tool"
	"time"
	"gitee.com/lzqysoft/framework/common/uuid"
	"gitee.com/lzqysoft/framework/common/log"
)

type UserAuthority struct {
	Id          string `xorm:"not null pk unique VARCHAR(64)"`
	Onwer       string `xorm:"VARCHAR(64)"`
	Group       string `xorm:"VARCHAR(64)"`
	Creater     string `xorm:"VARCHAR(64)"`
	CreateAt    int64  `xorm:"BIGINT"`
	CreateOrg   string `xorm:"VARCHAR(64)"`
	Updater     string `xorm:"VARCHAR(64)"`
	UpdateAt    int64  `xorm:"BIGINT"`
	Deleter     string `xorm:"VARCHAR(64)"`
	DeleteAt    int64  `xorm:"BIGINT"`
	Isdelete    string `xorm:"VARCHAR(2)"`
	Status      string `xorm:"VARCHAR(2)"`
	Remark      string `xorm:"VARCHAR(255)"`
	Operation   int    `xorm:"INTEGER"`
	Obligate3   string `xorm:"VARCHAR(64)"`
	Obligate2   string `xorm:"VARCHAR(64)"`
	Obligate    string `xorm:"VARCHAR(64)"`
	UserId      string `xorm:"index(index_15) VARCHAR(64)"`
	AuthorityId string `xorm:"index(index_15) VARCHAR(64)"`
}

func (v *UserAuthority) TableName() string {
	return "base_user_authority"
}

func (v *UserAuthority) BeforeInsert(){
	v.Id = uuid.UUID()
	v.CreateAt = time.Now().Unix()
	v.UpdateAt = time.Now().Unix()
}
func (v UserAuthority) Convert2Map() map[string]interface{} {
	return tool.Struct2Map(v)
}

func GetUserAuthorityByUserId(userId string) []UserAuthority{
	var groups []UserAuthority
	if err := db.Where("user_id = ?", userId).Find(&groups); err !=nil {
		log.Error(err)
		return nil
	}
	return groups
}